Ext.BLANK_IMAGE_URL = location.href.replace('main.js') + 'styles/images/default/s.gif';
var cmApp, CURRENTUSER;
// 登录窗口
var loginWin = new SHED.Window({
    id: 'loginWin',
    title: '登录窗口',
    width: 240,
    height: 130,
    draggable: false,
    resizable: false,
    closable: false,
    buttons: [{
        text: '登录',
        handler: function() {
            var form = loginWin.getBasicForm();
            if (form.isValid()) {
                var values = form.getValues();
                smDwr.login(values.userName, values.userPwd, function(result) {
                    if (result && result.flag) {
                        CURRENTUSER = result.user;
                        loginWin.hide();
                        initFun();
                    } else {
                        Ext.Msg.alert('登录失败', result.msg);
                        form.reset();
                        form.isValid();
                    }
                });
            } else {
                SHED.msg('表单中的字段未通过效验，请检查后再提交！');
            }
        }
    }],
    items: new SHED.form.DWRForm({
        dwr: {
            load: Ext.emptyFn,
            save: Ext.emptyFn
        },
        items: [{
            fieldLabel: '用户名称',
            name: 'userName',
            allowBlank: false,
            value: 'admin',
            validator: function(v) {
                return (/[\W]/).test(v) ? "用户名只能由英文字母、数字和下划线组成！" : true;
            }
        }, {
            fieldLabel: '用户密码',
            name: 'userPwd',
            allowBlank: false,
            inputType: 'password',
            enableKeyEvents: true,
            value: '000000',
            listeners: {
                'keypress': function(o, e) {
                    if (e.getKey() == e.ENTER) {
                        loginWin.buttons[0].handler();
                    }
                }
            }
        }]
    })
});

// 修改密码窗口
var pwdWin = new SHED.Window({
    id: 'pwdWin',
    title: '修改密码',
    width: 240,
    height: 155,
    draggable: false,
    resizable: false,
    items: new SHED.form.DWRForm({
        dwr: {
            load: Ext.emptyFn,
            save: function(values) {
                smDwr.updateUserPwd(CURRENTUSER.userID, values.userPwd, values.preUserPwd, function(result) {
                    if (result.flag && result.flag == 'true') {
                        smDwr.logoff(function() {
                            pwdWin.hide();
                            Ext.Msg.alert('提示', '密码修改成功，请以新的密码登录系统！', function() {
                                location.reload();
                            });
                        });
                    } else {
                        SHED.msg(result.msg);
                        pwdWin.getBasicForm().reset();
                        pwdWin.getBasicForm().isValid();
                    }
                    pwdWin.getForm().afterRemote();
                });
            }
        },
        items: [{
            fieldLabel: '当前密码',
            name: 'preUserPwd',
            allowBlank: false,
            inputType: 'password',
            minLength: 6,
            maxLength: 12
        }, {
            fieldLabel: '新 密 码',
            name: 'userPwd',
            allowBlank: false,
            inputType: 'password',
            minLength: 6,
            maxLength: 12
        }, {
            fieldLabel: '重复密码',
            name: 'reUserPwd',
            allowBlank: false,
            inputType: 'password',
            minLength: 6,
            maxLength: 12,
            validator: function(val) {
                if (pwdWin.getBasicForm().getValues().userPwd != val) {
                    return '两次输入的密码不一致！';
                } else {
                    return true;
                }
            }
        }]
    })
});

var initFun = function() {
    Ext.get('modiinfo').update(CURRENTUSER.userName);
    
    var center = new Ext.TabPanel({
        region: 'center',
        contentEl: 'center',
        activeItem: 0,
        deferredRender: false,
        enableTabScroll: true,
        monitorResize: true,
        margins: '0 0 2 2',
        cmargins: '0 0 2 2',
        items: [new Ext.Panel({
            title: '首页',
            id: 'welcome_Tab',
            html: '<h1>欢迎使用S.H.E.D.</h1><p>这里应该放一些介绍性的文字，不过暂时还没有考虑好！^_^</p><p>本网站的系统为纯界面演示，所有数据库操作均未实现。</p>'
        })]
    });
    
    var west = new Ext.tree.TreePanel({
        rootVisible: false,
        region: 'west',
        id: 'west-panel',
        title: '系统菜单',
        useArrows: true,
        animCollapse: false,
        stateful: false,
        width: 180,
        autoScroll: true,
        collapsible: true,
        margins: '0 0 2 0',
        cmargins: '0 0 2 0',
        loader: new SHED.tree.DWRTreeLoader({
            dwrCall: smDwr.getMenu,
            nodeMeta: {
                id: 'menuID',
                text: 'menuName',
                leaf: 'menuIsLeaf'
            }
        }),
        root: {
            nodeType: 'async',
            text: '系统菜单',
            draggable: false,
            id: '0',
            menuID: '0',
            menuName: '系统菜单',
            leaf: false
        },
        listeners: {
            'click': function(node) {
                if (node.attributes.leaf == 1) {
                    var moduleId = 'tab_' + node.attributes.menuID;
                    if (cmApp.body.findById(moduleId)) {
                        cmApp.body.findById(moduleId).show();
                        return;
                    }
                    
                    cmApp.loadMask.show();
                    var panel = cmApp.body.add({
                        id: moduleId,
                        title: node.attributes.menuName,
                        closable: true,
                        layout: 'fit'
                    }).show();
                    Ext.Ajax.request({
                        method: 'GET',
                        url: 'modules/' + node.attributes.menuUrl,
                        success: function(response) {
                            var module = eval(response.responseText);
                            cmApp[moduleId] = new module(panel);
                            cmApp.loadMask.hide();
                        }
                    });
                }
            }
        }
    });
    
    var north = new Ext.BoxComponent({
        region: 'north',
        el: 'north',
        height: 32
    });
    
    var south = new Ext.Panel({
        region: 'south',
        el: 'south',
        autoScroll: true,
        height: 200,
        minSize: 100,
        maxSize: 300,
        collapsible: true,
        animCollapse: false,
        collapsed: true,
        collapsedTitle: '系统提示信息',
        title: '系统提示信息',
        margins: '0',
        cmargins: '0'
    });
    
    cmApp = new SHED.app(north, south, west, center);
    
    var loading = Ext.get('loading');
    var mask = Ext.get('loading-mask');
    mask.setOpacity(0.8);
    mask.shift({
        xy: loading.getXY(),
        width: loading.getWidth(),
        height: loading.getHeight(),
        remove: true,
        duration: 1,
        opacity: 0.3,
        easing: 'bounceOut',
        callback: function() {
            loading.fadeOut({
                duration: 0.2,
                remove: true
            });
        }
    });
    
    Ext.get('modify').on('click', function() {
        pwdWin.show();
    });
    Ext.get('logoff').on('click', function() {
        Ext.Msg.confirm('确认', '您确定要退出本系统吗？', function(para) {
            if (para == 'yes') {
                smDwr.logoff(function() {
                    location.reload();
                });
            }
        });
    });
};
Ext.onReady(function() {
    Ext.QuickTips.init();
    
    smDwr.getCurrentUser({
        callback: function(user) {
            if (user.userID !== undefined && user.userID !== null) {
                CURRENTUSER = user;
                initFun();
            } else {
                loginWin.show();
            }
        }
    });
});

